package com.javachmapi.pay.config;


import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 *  通过java代码配置Swagger
 *  @Configuration表示是配置文件
 *  @EnableSwagger2开启这个配置
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    UiConfiguration uiConfig() {
        return new UiConfiguration(null, // url
                "list", // docExpansion => none | list
                "alpha", // apiSorter => alpha
                "schema", // defaultModelRendering => schema
                UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, // enableJsonEditor => true | false
                true, null);
    }

    /**
     * 定义api组，
     */
    @Bean
    public Docket adminApi() {
        List<Parameter> parameterList = Lists.newArrayList();
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("支付后台API接口文档")
                .enable(true)
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .globalOperationParameters(parameterList)
                .forCodeGeneration(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.javachmapi.pay.modules.admin.controller"))
                .paths(PathSelectors.any())
                .build()
                .directModelSubstitute(boolean.class, Boolean.class)
                .apiInfo(apiInfo());
    }

    /**
     * 定义api组，
     */
    @Bean
    public Docket alipayApi() {
        List<Parameter> parameterList = Lists.newArrayList();
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("支付宝API接口文档")
                .enable(true)
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .globalOperationParameters(parameterList)
                .forCodeGeneration(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.javachmapi.pay.modules.alipay.controller"))
                .paths(PathSelectors.any())
                .build()
                .directModelSubstitute(boolean.class, Boolean.class)
                .apiInfo(apiInfo());
    }

    /**
     * 定义api组，
     */
    @Bean
    public Docket wxpayApi() {
        List<Parameter> parameterList = Lists.newArrayList();
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("微信API接口文档")
                .enable(true)
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .globalOperationParameters(parameterList)
                .forCodeGeneration(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.javachmapi.pay.modules.wxpay.controller"))
                .paths(PathSelectors.any())
                .build()
                .directModelSubstitute(boolean.class, Boolean.class)
                .apiInfo(apiInfo());
    }

    /**
     * 定义api组，
     */
    @Bean
    public Docket unionpayApi() {
        List<Parameter> parameterList = Lists.newArrayList();
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("银联API接口文档")
                .enable(true)
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .globalOperationParameters(parameterList)
                .forCodeGeneration(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.javachmapi.pay.modules.unionpay.controller"))
                .paths(PathSelectors.any())
                .build()
                .directModelSubstitute(boolean.class, Boolean.class)
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("支付系统")//大标题
                .description("微信、支付宝、银联支付服务")//详细描述
                .version("1.0")//版本
                .termsOfServiceUrl("http://pay.aizhuoyue.net")
                .contact(new Contact("宋利军", "http://pay.aizhuoyue.net", "javachmapi@qq.com"))
                .build();
    }
}
